package com.yuneec.fly.utils;

import android.os.SystemClock;
import android.text.TextUtils;

import com.yuneec.droneservice.command.CommandListener;
import com.yuneec.droneservice.request.IRequest;
import com.yuneec.droneservice.utils.ByteUtils;
import com.yuneec.fly.DroneApplication;
import com.yuneec.fly.events.EventBusFactory;
import com.yuneec.fly.events.commandEvent.SendCommandEvent;
import com.yuneec.fly.events.commandEvent.SetAnalogAndSwitchChannelCountEvent;
import com.yuneec.fly.models.ConnectResource;
import com.yuneec.fly.models.DroneModels;
import com.yuneec.fly.models.ST16ChannelConfig;

/**
 * Created by gjw on 2016/12/6.
 */

public class DroneConnectHelper {

    public static void sendCommand(IRequest command, CommandListener listener) {
        EventBusFactory.getCommandBus().post(new SendCommandEvent(command, listener));
    }


    public static void connectDrone(final ConnectResource resource) {
//        sendCommand(new ExitRun(),null);
        SystemClock.sleep(100);
//        sendRecvBothCh(resource,channelConfigs);
        RxHelper.sendRx(resource.getRadioResource());
        SystemClock.sleep(100);
        EventBusFactory.getCommandBus().post(new SetAnalogAndSwitchChannelCountEvent(resource.getAnalogChannelNumber(), resource.getSwitchChannelNumber()));

//        sendCommand(new EnterRun(), null);
        LogUtils.e("connect to drone finish");
    }
//    public static void connectDrone(final ConnectResource resource, final ArrayList<byte[]> channelConfigs) {
//        sendCommand(new ExitRun(),null);
//        SystemClock.sleep(100);
////        sendRecvBothCh(resource,channelConfigs);
//        RxHelper.sendRx(resource.getRadioResource());
//        SystemClock.sleep(100);
//        sendCommand(new EnterRun(), null);
//        LogUtils.e("connect to drone finish");
//    }

//    private static void sendRecvBothCh(final ConnectResource resource, final ArrayList<byte[]> channelConfigs) {
//        sendCommand(new RecvBothCh(), new CommandListenerAdapter() {
//            @Override
//            public void onSuccess(BaseResponse response) {
////                sendAllDataToFlightControl(resource, channelConfigs);
//                RxHelper.sendRx(resource.getRadioResource());
//                SystemClock.sleep(100);
//                sendCommand(new EnterRun(), null);
//                LogUtils.e("connect to drone finish");
//            }
//        });
//    }

//    private static void sendAllDataToFlightControl(final ConnectResource flightModePassingParams, final ArrayList<byte[]> channelConfigs) {
//        sendCommand(new SetChannelSetting(flightModePassingParams.getAnalogChannelNumber(), flightModePassingParams.getSwitchChannelNumber()), new CommandListenerAdapter() {
//            @Override
//            public void onSuccess(BaseResponse response) {
//                afterSetChannelSetting(channelConfigs, true);
//            }
//
//            @Override
//            public void onTimeout() {
//                afterSetChannelSetting(channelConfigs, false);
//            }
//
//            @Override
//            public void onError(int errorCode) {
//                afterSetChannelSetting(channelConfigs, false);
//            }
//        });
//    }

//    private static void afterSetChannelSetting(final ArrayList<byte[]> channelConfigs, boolean success) {
//        if (success) {
//            afterSetChannelSettingAction(channelConfigs);
//        } else {
//            sendCommand(new SyncMixingDataDeleteAll(), new CommandListenerAdapter() {
//                @Override
//                public void onSuccess(BaseResponse response) {
//                    afterSetChannelSettingAction(channelConfigs);
//                }
//
//                @Override
//                public void onError(int errorCode) {
//                    afterSetChannelSettingAction(channelConfigs);
//                }
//
//                @Override
//                public void onTimeout() {
//                    afterSetChannelSettingAction(channelConfigs);
//                }
//            });
//        }
//
//    }

//    private static void afterSetChannelSettingAction(final ArrayList<byte[]> channelConfigs) {
//        for (byte[] data : channelConfigs) {
//            sendCommand(new SyncMixingDataAdd(data), null);
//        }
//    }

    public static void connectLastDrone() {
        String rxString = SharePrefUtil.getString(DroneApplication.getApplication(), SharePrefUtil.KEY.KEY_LAST_CONNECTED_DRONE_RX, "");
        if (TextUtils.isEmpty(rxString)) {
            return;
        }
        ConnectResource resource = new ConnectResource();
        resource.setRadioResource(ByteUtils.bits2bytes(rxString));
        resource.setAnalogChannelNumber(DroneModels.H480.ANALOGCH_NUMBER);
        resource.setSwitchChannelNumber(DroneModels.H480.SWITCHCH_NUMBER);
        resource.setChannelConfigListSize(ST16ChannelConfig.configsList.size());
        connectDrone(resource);
    }
}
